import java.awt.Checkbox;


import java.awt.Color;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import common.ChatIF;

import client.AESSClient;
import client.ProfessorClient;

import users.Prof;





public class ProfessorGUI extends JFrame implements ChatIF{
//	public Prof user;
	
	public TimetablePanel tp;
	ProfessorClient proclient;
	

	
	public ProfessorGUI(String host, int port, ChatIF professorUI, Object user){
		try {
			proclient= new ProfessorClient(host, port, professorUI);
			proclient.user = (Prof)user;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JTabbedPane jtp = new JTabbedPane();
		
		
		tp = new TimetablePanel(proclient);
		LecRoomPanel lp = new LecRoomPanel(proclient);
		PrefertimePanel pp = new PrefertimePanel(proclient);
		TestTAPanel ap = new TestTAPanel(proclient);
		StdInputCheckPanel sp = new StdInputCheckPanel(proclient);
	
		
		jtp.addTab("수업시간표확인", tp);
		jtp.addTab("강의실입력", lp);
		jtp.addTab("선호시간대", pp);
		jtp.addTab("시험감독", ap);
		jtp.addTab("수강생입력조회", sp);
		
		
		getContentPane().add(jtp);
		
		
		
		
		setTitle("Processor");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1000,800);
		setVisible(true);
		
	}



	@Override
	public void display(String message) {
		// TODO Auto-generated method stub
		
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TimetablePanel extends JPanel implements ActionListener{

	JLabel jl1= new JLabel("수업시간표를 출력합니다.");
	JLabel jl2= new JLabel("시험기간에 수업을 하지않을경우 체크를 해주세요.");
	Checkbox jrb1 = new Checkbox("NotClass");
	JButton outputTimetable=new JButton("Display");
	JButton save=new JButton("저장");
	public TextArea display = new TextArea("");
	ProfessorClient proclient;
	// ProfessorClient pc = new ProfessorClient(여기에 로그인 정보);
	
	public TimetablePanel(ProfessorClient proclient){
		
		
		this.proclient=proclient;
		setPanel();
		setButton();
		
		
		
		
		
	}
	public void actionPerformed(ActionEvent ae){ //체크를하면
		
		String str = ae.getActionCommand();
		
		if(str.equals("Display")){
			
				proclient.requireTimeTable();
				display.setText(proclient.getTimeTable());  
			
		}
		else if(str.equals("저장")){
			if(jrb1.getState()){
				proclient.setClassnot();
			}
			
		}
		else{
			System.exit(0);
		}
	}
	
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	public void setButton()
	{
		outputTimetable.addActionListener(this);
		save.addActionListener(this);
		display.setEditable(false);

		add(jl1);
		// pc.checkTime();
		add(jl2);
		add(jrb1);
		add(display);
		add(save);
		add(outputTimetable);
		
		             
		  jl1.setBounds(300, 0, 180 , 50);
		  jl2.setBounds(300, 400, 500, 50 );
		  jrb1.setBounds(300, 450, 150, 50);
		  save.setBounds(400, 600, 200, 100);
		  display.setBounds(300, 50, 400, 200);
		  outputTimetable.setBounds(480, 10, 150, 30);
	}
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





class LecRoomPanel extends JPanel implements ActionListener{
	
	JLabel jl1= new JLabel("최대수용인원 입력.");
	TextField t1 = new TextField("");
	JLabel jl2= new JLabel("강의실 개수 입력.");
	TextField t2 = new TextField("");
	JLabel jl3= new JLabel("시험시간 입력.");
	TextField t3 = new TextField("");
	
	JButton save=new JButton("저장");
	ProfessorClient proclient;
	 
	 
	public LecRoomPanel(ProfessorClient proclient){
		this.proclient=proclient;
		setPanel();
		setButton();
		
		
	}
	public void actionPerformed(ActionEvent ae){
		String str = ae.getActionCommand();
		
		if(str.equals("저장")){
			
			proclient.setMaxCapacity(Integer.parseInt(t1.getText()));
			proclient.setNumberofRoom(Integer.parseInt(t2.getText()));
			proclient.setTimeofTest(Integer.parseInt(t3.getText()));
			proclient.sendMaxcapacity();
			proclient.sendNumberofRoom();
			proclient.sendTimeofTest();
		}
		else{
			System.exit(0);
		}
	}
	
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	public void setButton()
	{
		
		save.addActionListener(this);

		add(jl1);
		add(t1);
		add(jl2);
		add(t2);
		add(jl3);
		add(t3);
		add(save);
		
		  jl1.setBounds(300, 0, 300 , 50);
		  t1.setBounds(300, 50, 300 , 20);
		  jl2.setBounds(300, 200, 300, 50 );
		  t2.setBounds(300, 250, 300 , 20);
		  jl3.setBounds(300, 400, 300, 50);
		  t3.setBounds(300, 450, 300 , 20);
		  save.setBounds(400, 600, 200, 100);
	  
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class PrefertimePanel extends JPanel implements ActionListener{
	
	JLabel jl1= new JLabel("희망하는 시간대 1순위.");
	TextField t1 = new TextField("");
	JLabel jl2= new JLabel("희망하는 시간대 2순위.");
	TextField t2 = new TextField("");
	JLabel jl3= new JLabel("희망하는 시간대 3순위.");
	TextField t3 = new TextField("");
	JLabel jl4= new JLabel("희망하는 시간대 4순위.");
	TextField t4 = new TextField("");
	JLabel jl5= new JLabel("희망하는 시간대 5순위.");
	TextField t5 = new TextField("");
	JLabel jl6= new JLabel("희망하지 않는 시간대.");
	TextField t6 = new TextField("");
	
	JButton save=new JButton("저장");
	ProfessorClient proclient;
	 
	public PrefertimePanel(ProfessorClient proclient){
		this.proclient=proclient;
		setPanel();
		setButton();
		
		
	}
	public void actionPerformed(ActionEvent ae){
		
		String str = ae.getActionCommand();
		
		if(str.equals("저장")){
			proclient.setPreferTime(t1.getText(),t2.getText(),t3.getText(),t4.getText(),t5.getText());
			proclient.sendPreferTime();
		}
		else{
			System.exit(0);
		}
		
	}
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	public void setButton()
	{
		
		save.addActionListener(this);

		add(jl1);
		add(t1);
		add(jl2);
		add(t2);
		add(jl3);
		add(t3);
		add(jl4);
		add(t4);
		add(jl5);
		add(t5);
		add(jl6);
		add(t6);
		add(save);
		
		  jl1.setBounds(300, 0, 300 , 50);
		  t1.setBounds(300, 50, 300 , 20);
		  jl2.setBounds(300, 100, 300, 50 );
		  t2.setBounds(300, 150, 300 , 20);
		  jl3.setBounds(300, 200, 300, 50);
		  t3.setBounds(300, 250, 300 , 20);
		  jl4.setBounds(300, 300, 300 , 50);
		  t4.setBounds(300, 350, 300 , 20);
		  jl5.setBounds(300, 400, 300 , 50);
		  t5.setBounds(300, 450, 300 , 20);
		  jl6.setBounds(300, 500, 300 , 50);
		  t6.setBounds(300, 550, 300 , 20);
		  save.setBounds(400, 600, 200, 100);
	  
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


class TestTAPanel extends JPanel implements ActionListener{
	
	JLabel jl1= new JLabel("조교 학번 입력.");
	TextField t1 = new TextField("");
	JLabel jl2= new JLabel("조교 이름 입력.");
	TextField t2 = new TextField("");
	TextArea display = new TextArea("");
	
	
	
	JButton save=new JButton("감독위임");
	JButton quary=new JButton("조회");
	JButton delete=new JButton("삭제");
	ProfessorClient proclient;
	
	public TestTAPanel(ProfessorClient proclient){
		this.proclient=proclient;
		setPanel();
		setButton();
		
	}
	public void actionPerformed(ActionEvent ae){
		
		String str = ae.getActionCommand();
		String str1;
		
		if(str.equals("감독위임")){
		//	pc.setPreferTime(t1.gettext(),t2.gettext(),t3.gettext(),t4.gettext(),t5.gettext());
	
			proclient.setTA(t1.getText()); 
			proclient.sendTA();
			display.setText("감독이 성공적으로 위임되었습니다.");   //만일 감독을 안하고있다면 감독 위임
			 
			 
			
		}
		else if(str.equals("조회")){
			proclient.setTA(t1.getText()); 
			proclient.sendexistTA();             //학번 존재하는지 확인요청
			str1 = proclient.getexistTA();
			 if(str1 == "yes"){
				 display.setText("현재 감독입니다.");  
			 }
			 else{
				 display.setText("현재 감독이 아닙니다.");   
			 }
		
		}
		
		else if(str.equals("삭제")){
			
			proclient.setTA(t1.getText()); 
			proclient.senddeleteTA();  
			
			
			display.setText("감독위임이 해제되었습니다.");  
			 
		
		}
		else{
			System.exit(0);
		}
	}
	
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	public void setButton()
	{
		
		save.addActionListener(this);
		quary.addActionListener(this);
		delete.addActionListener(this);
		display.setEditable(false);          //수정못하게 

		add(jl1);
		add(t1);
		add(jl2);
		add(t2);
		add(display);
	
		add(save);
		add(quary);
		add(delete);
		
		  jl1.setBounds(100, 100, 300 , 50);
		  t1.setBounds(100, 150, 300 , 20);
		  jl2.setBounds(100, 200, 300, 50 );
		  t2.setBounds(100, 250, 300 , 20);
		  display.setBounds(500, 100, 400 , 400);
		  save.setBounds(100, 600, 200, 100);
		  quary.setBounds(400, 600, 200, 100);
		  delete.setBounds(700, 600, 200, 100);
	  
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class StdInputCheckPanel extends JPanel implements ActionListener{
	
	private JTable jTable1;
	private JTable jTable2;
	public TableModel jTable1Model;
	public TableModel jTable1Mode2;
	
	
	static int row1, col1;
	static int row2, col2;
	
	JLabel jl1= new JLabel("총 수강 인원.");
	TextField t1 = new TextField("");
	JLabel jl2= new JLabel("스케줄 입력한 인원.");
	TextField t2 = new TextField("");
	JLabel jl3= new JLabel("입력도 다 안했는데 이러시면 안됩니다.");
	JLabel jl4= new JLabel("모든 수강생이 스케줄을 입력하였습니다.");
//	TextArea display1 = new TextArea("");
//	TextArea display2 = new TextArea("");
	
	
	JButton numofpeople=new JButton("조회1");
	JButton numofcurrent=new JButton("조회2");
	JButton quaryTime=new JButton("시간조회");
	JButton selectTime=new JButton("시간선택");
	JButton selectRoom=new JButton("장소선택");
	JButton quaryRoom=new JButton("장소조회");
	ProfessorClient proclient;
	
	public StdInputCheckPanel(ProfessorClient proclient){
		this.proclient=proclient;
		setPanel();
		initGUI();
		setButton();
		
		
	}
	public void actionPerformed(ActionEvent ae){
		
		String str = ae.getActionCommand();
		String a,b = null;
		String []time = new String[6];
		String []day = new String[6];
		String []room = new String[6];
		
		if(str.equals("조회1")){
			proclient.requestTotalNum();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			a=proclient.getTotalNum();
			
			t1.setText(a);
            
			 
		 }
		
	
	
	
		else if(str.equals("조회2")){
			
			proclient.requestCurrentNum();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			b=proclient.getCurrentNum();
			boolean bool = proclient.getCurrentNum();
			
			t1.setText(b);
            
	
			
		              
//			 if(t1.getText()==t2.getText()){              //수강생 입력 수에 따른 라벨 출력
			 if(proclient.getCurrentNum() ){              //수강생 입력 수에 따른 라벨 출력

					add(jl4);
					jl4.setBounds(100,450,250,20);
				}
				else{
					add(jl3);
					jl3.setBounds(100,450,250,20);
				}
			
			 
			
			
		}
	
		
		else if(str.equals("시간조회")){
	
			proclient.inquiryTime();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			time = (proclient.getPossibleTime());
			day = proclient.getPossibleDay();
			jTable1Model.setValueAt(day[0]+"일 "+time[0], 0, 0);
			jTable1Model.setValueAt(day[1]+"일 "+time[1], 1, 0);
			jTable1Model.setValueAt(day[2]+"일 "+time[2], 2, 0);
			jTable1Model.setValueAt(day[3]+"일 "+time[3], 3, 0);
			jTable1Model.setValueAt(day[4]+"일 "+time[4], 4, 0);
			jTable1Model.setValueAt(day[5]+"일 "+time[5], 5, 0);
	//			 display1.setText(time);   //모든 수강생 정보 종합해 나온 점수
			 }
			
		else if(str.equals("시간선택")){
			
			
			proclient.sendSelectTime(jTable1Model.getValueAt(row1, col1));
			
			             
	//			 display1.setText(time);   //모든 수강생 정보 종합해 나온 점수
			 }
		
		
		else if(str.equals("장소조회")){
		
			proclient.inquiryRoom();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			room = proclient.getPossibleRoom();
			jTable1Mode2.setValueAt(room[0], 0, 0);
			jTable1Mode2.setValueAt(room[1], 1, 0);
			jTable1Mode2.setValueAt(room[2], 2, 0);
			jTable1Mode2.setValueAt(room[3], 3, 0);
			jTable1Mode2.setValueAt(room[4], 4, 0);
			jTable1Mode2.setValueAt(room[5], 5, 0);

		//		 display2.setText(room);   //장소 정보 종합해 나온 장소 출력
				 
				
				
			}
		
		else if(str.equals("장소선택")){
			
			proclient.sendSelectRoom(jTable1Mode2.getValueAt(row2, col2));
		}
		else{
			System.exit(0);
		}
		
	}
	public void setPanel()
	{
		setLayout(null);
		setBackground(Color.white);
	 
	}
	
	private void initGUI() {
		try {
//			setPreferredSize(new Dimension(400, 300));
			{
				jTable1Model = 
						new DefaultTableModel(
								new String[][] { {"리스트1"}, 
										{"리스트2"}, 
										{"리스트3"}, 
										{"리스트4"}, 
										{"리스트5"}, 
										{"리스트6"}, 
										
						
								},
								new String[] { "Column 1"});
				jTable1Mode2 = 
						new DefaultTableModel(
								new String[][] { {"리스트1"}, 
										{"리스트2"}, 
										{"리스트3"}, 
										{"리스트4"}, 
										{"리스트5"}, 
										{"리스트6"}, 
										
						
								},
								new String[] { "Column 1"});
				jTable1 = new JTable();
				jTable2 = new JTable();
				this.add(jTable1);
				this.add(jTable2);
				jTable1.setModel(jTable1Model);
				jTable2.setModel(jTable1Mode2);
		//		jTable1.setPreferredSize(new java.awt.Dimension(343, 288));
				jTable1.setBounds(400,100,200,400);
				jTable2.setBounds(700, 100, 200 , 400);
			//	
				jTable1.setRowHeight(70);
				jTable2.setRowHeight(70);
				jTable1.getColumnModel().getColumn(1).setWidth(20);
				jTable2.getColumnModel().getColumn(1).setWidth(20);
				//jTable1.set
			
				jTable1.addMouseListener(new MouseAdapter() {
					 public void mouseClicked(MouseEvent evt) {
					         row1 = jTable1.rowAtPoint(evt.getPoint());
					         col1 = jTable1.columnAtPoint(evt.getPoint());
					        if (row1 >= 0 && col1 >= 0) {
					        //	System.out.println(row +","+col);
					        }
					
				}});
				
				jTable2.addMouseListener(new MouseAdapter() {
					 public void mouseClicked(MouseEvent evt) {
					         row2 = jTable2.rowAtPoint(evt.getPoint());
					         col2 = jTable2.columnAtPoint(evt.getPoint());
					        if (row2 >= 0 && col2 >= 0) {
					        //	System.out.println(row +","+col);
					        }
					
				}});
					
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void setButton()
	{
		t1.setEditable(false);
		t2.setEditable(false);
	//	display1.setEditable(false);    
	//	display2.setEditable(false);    
		t1.setText(""); //  수강생 총원정보 String 로 받아와서 입력 ex.a  *********************
		t2.setText("");  // 수강생중 스케줄 입력한 학생수 입력  ex.b
		quaryTime.addActionListener(this);
		quaryRoom.addActionListener(this);
		numofpeople.addActionListener(this);
		numofcurrent.addActionListener(this);
		selectTime.addActionListener(this);
		selectRoom.addActionListener(this);
		
		add(jl1);
		add(t1);
		add(jl2);
		add(t2);
		add(selectTime);
		add(selectRoom);
		
	

		
	//	add(display1);
	//	add(display2);
		add(quaryTime);
		add(quaryRoom);
		add(numofpeople);
		add(numofcurrent);
		
		  jl1.setBounds(100, 100, 150 , 20);
		  t1.setBounds(250, 100, 100 , 20);
		  
		  jl2.setBounds(100, 300, 150, 20 );
		  t2.setBounds(250, 300, 100 , 20);
		 
	//	  display1.setBounds(400, 100, 200 , 400);
	//	  display2.setBounds(700, 100, 200 , 400);
		  quaryTime.setBounds(400, 600, 100, 40);
		  selectTime.setBounds(520, 600, 100, 40);
		  quaryRoom.setBounds(700, 600, 100, 40);
		  selectRoom.setBounds(820, 600, 100, 40);
		  numofpeople.setBounds(250, 140, 80 , 20);
		  numofcurrent.setBounds(250, 340, 80 , 20);
	  
	}
}


